package cmd

import (
	"bytes"
	"fmt"
	"gitee.com/lllgo/opsctl/pkg/conf"
	"github.com/spf13/cobra"
	"io/ioutil"
)

func configCmd() *cobra.Command {
	var recursive bool
	var tmp = &cobra.Command{
		Use:   "config",
		Short: "显示配置信息",
		Run: func(cmd *cobra.Command, args []string) {
			sb := bytes.NewBufferString("")

			writeConfig(sb, "属性配置文件:", conf.ConfigPath, recursive)
			writeConfig(sb, "服务器配置文件:", conf.InventoryPath, recursive)
			writeConfig(sb, "插件配置文件:", conf.PluginPath, recursive)
			fmt.Println(sb.String())
		},
	}
	tmp.Flags().BoolVarP(&recursive, "recursive", "r", false, "打印配置文件内容")
	return tmp
}

func writeConfig(sb *bytes.Buffer, prefix, cfg string, recursive bool) {
	sb.WriteString(prefix + cfg + "\n")
	if recursive {
		bs, _ := ioutil.ReadFile(cfg)
		sb.Write(bs)
		sb.WriteString("\n\n")
	}
}
